<?php

namespace app\widgets;

use yii\widgets\InputWidget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
 * 日历小部件
 * Class EditorWidget
 * @package app\widgets
 */
class JeDate extends InputWidget
{
    /**
     * @var array the HTML attributes for the input tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $options = ['class' => 'form-control'];

    /**
     * @var array 返回一个日历设置
     */
    public $dateOptions = [];
    /**
     * @var string the template for arranging the CAPTCHA image tag and the text input tag.
     * In this template while `{input}` will be replaced with the text input tag.
     */
    public $template = '{input}';

    public $format = null;
    public $valueFormat = null;
    public $chooseFun = null;
    public function init()
    {
        parent::init();

        if (!isset($this->dateOptions['id'])) {
            $this->dateOptions['id'] = $this->options['id'] ;
        }
        if(!$this->format){
            $this->format = "YYYY-MM-DD";
        }
        if($this->valueFormat){
            if ($this->hasModel()) {
                $attribute = $this->attribute;
                $model = $this->model;
                $this->options['value'] = is_integer($model->$attribute)?($model->$attribute>1900800?Date($this->valueFormat,floatval($model->$attribute)):''):$model->$attribute;
            }else{
                $this->options['value'] = Date($this->valueFormat, $this->value);
            }
        }
    }
    public function run()
    {
        $this->registerClientScript();
        if ($this->hasModel()) {
            $input = Html::activeTextInput($this->model, $this->attribute, $this->options);
        } else {
            $input = Html::textInput($this->name, $this->value, $this->options);
        }
        echo strtr($this->template, [
            '{input}' => $input,
        ]);
    }

    /**
     * Registers the needed JavaScript.
     */
    public function registerClientScript()
    {
        $id = $this->dateOptions['id'];
        $options = $this->getClientOptions($id);
        $options = empty($options) ? '' : $this->__json_encode($options);
        $view = $this->getView();
        JeDateAsset::register($view);
        $name =str_replace('-','',$id);
        $view->registerJs("optionsDate{$name} = $options; jeDate(optionsDate{$name});");
    }

    protected function __json_encode($mVar) {
        return preg_replace_callback(
            '#"[ ]{0,}function[ ]{0,}\([^)$]{0,}\)[ ]{0,}\{[ ]{0,}(?(?![ ]{0,}}[ ]{0,}").){0,}[ ]{0,}\}[ ]{0,}"#si',
            function ($aRes) {
                for($aRes[0]=substr($aRes[0],1),$sOut="",$i=0,$iOpen=0,$iClose=0;$i<= strlen($aRes[0]) && $sOut.= substr($aRes[0],$i,1);$i++)
                    if (substr($aRes[0],$i,1) == "{") $iOpen++;
                    else if (substr($aRes[0],$i,1) == "}" AND $iOpen == ++$iClose) break;
                return stripcslashes($sOut);
            },
            Json::htmlEncode($mVar)
        );
    }

    /**
     * Returns the options for the JS widget.
     * @return array the options
     */
    protected function getClientOptions($id)
    {
        $options = [
            'dateCell'=> "#$id", //isinitVal:true,
            'format'=> $this->format,
            'isTime'=> false,

            //'isClear'=>true,
        ];
        if($this->chooseFun){
            $options['choosefun'] = $this->chooseFun;
        }

        return $options;
    }
}